package mvc_generator.generator.car.struts;

import mvc_generator.generator.MVCGeneratorGeneratorUtils;
import mvc_generator.generator.car.struts.StrutsGenerator;
import mvc_generator.mVCGenerator.AssignedField;
import mvc_generator.mVCGenerator.Form;
import mvc_generator.mVCGenerator.ModelMVC;
import mvc_generator.mVCGenerator.View;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;

@SuppressWarnings("all")
public class ViewGenerator {
  private StrutsGenerator g;
  
  public ViewGenerator(final StrutsGenerator gen) {
    this.g = gen;
  }
  
  public void generate(final View view, final IFileSystemAccess fsa) {
    ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(view);
    StringConcatenation _builder = new StringConcatenation();
    CharSequence jspContent = _builder;
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\"");
    _builder_1.newLine();
    _builder_1.append("pageEncoding=\"ISO-8859-1\"%>");
    _builder_1.newLine();
    _builder_1.append("<%@ taglib prefix=\"html\" uri=\"http://struts.apache.org/tags-html\"%>");
    _builder_1.newLine();
    _builder_1.append("<%@ taglib prefix=\"bean\" uri=\"http://struts.apache.org/tags-bean\"%>");
    _builder_1.newLine();
    _builder_1.append("<%@ taglib prefix=\"nested\" uri=\"http://struts.apache.org/tags-nested\"%>");
    _builder_1.newLine();
    _builder_1.append("<html:html>");
    _builder_1.newLine();
    _builder_1.append("<head>");
    _builder_1.newLine();
    _builder_1.append("<title><bean:message key=\"");
    String _name = view.getName();
    _builder_1.append(_name, "");
    _builder_1.append(".title\" /></title>");
    _builder_1.newLineIfNotEmpty();
    _builder_1.append("<html:base />");
    _builder_1.newLine();
    _builder_1.append("</head>");
    _builder_1.newLine();
    _builder_1.append("<body bgcolor=\"white\">");
    _builder_1.newLine();
    {
      EList<Form> _forms = view.getForms();
      int _size = _forms.size();
      boolean _equals = (_size == 0);
      if (_equals) {
      } else {
        {
          EList<Form> _forms_1 = view.getForms();
          for(final Form form : _forms_1) {
            _builder_1.append("<html:form action=\"/actionForm");
            String _name_1 = view.getName();
            _builder_1.append(_name_1, "");
            String _name_2 = form.getName();
            _builder_1.append(_name_2, "");
            _builder_1.append("\">");
            _builder_1.newLineIfNotEmpty();
            _builder_1.append("<html:errors />");
            _builder_1.newLine();
            _builder_1.append("</html:form>");
            _builder_1.newLine();
            _builder_1.append("<table>");
            _builder_1.newLine();
            {
              EList<AssignedField> _assignedFields = form.getAssignedFields();
              for(final AssignedField assignedField : _assignedFields) {
                _builder_1.append("<tr>");
                _builder_1.newLine();
                _builder_1.append("<td align=\"right\">");
                String _fieldName = assignedField.getFieldName();
                _builder_1.append(_fieldName, "");
                _builder_1.append("</td>");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("<td align=\"left\"><html:text property=\"");
                String _fieldName_1 = assignedField.getFieldName();
                _builder_1.append(_fieldName_1, "");
                _builder_1.append("\" size=\"30\"");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("maxlength=\"30\" /></td>");
                _builder_1.newLine();
                _builder_1.append("</tr>");
                _builder_1.newLine();
              }
            }
            _builder_1.append("<tr>");
            _builder_1.newLine();
            _builder_1.append("<td align=\"right\"><html:submit>Save</html:submit></td>");
            _builder_1.newLine();
            _builder_1.append("</tr>");
            _builder_1.newLine();
            _builder_1.append("</table>");
            _builder_1.newLine();
          }
        }
      }
    }
    _builder_1.append("</body>");
    _builder_1.newLine();
    _builder_1.append("</html:html>");
    _builder_1.newLine();
    jspContent = _builder_1;
    String _name_3 = modelMVCContainer.getName();
    String _plus = (_name_3 + "Struts");
    String _plus_1 = (_plus + StrutsGenerator.pagesPath);
    String _name_4 = view.getName();
    String _plus_2 = (_plus_1 + _name_4);
    String _plus_3 = (_plus_2 + ".jsp");
    fsa.generateFile(_plus_3, jspContent);
    String _name_5 = view.getName();
    String _plus_4 = (_name_5 + ".title=");
    String _name_6 = view.getName();
    String _plus_5 = (_plus_4 + _name_6);
    this.g.addLineToResourcesContentFile(_plus_5);
  }
}
